package com.czw.web.activity

import android.graphics.Bitmap
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import android.webkit.JavascriptInterface
import com.blankj.utilcode.util.LogUtils
import com.czw.web.databinding.LibActivityWebBinding
import com.mtzqc.common.kt.onClick
import com.mtzqc.common.ui.activity.ViewBindingActivity
import com.tencent.smtt.sdk.WebChromeClient
import com.tencent.smtt.sdk.WebView
import com.tencent.smtt.sdk.WebViewClient


class WebActivity : ViewBindingActivity<LibActivityWebBinding>(LibActivityWebBinding::inflate) {
    var webView: WebView? = null
    override fun setContentView() {
        super.setContentView()
        webView = binding.webView
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        initSettings()
        pageNavigator(View.GONE)
        webView?.loadUrl("http://gugux.caigetuxun.com/v3/redrain/#/")
        binding?.apply {
            backIv.onClick {
                if (true== this@WebActivity.webView?.canGoBack()) {
                    this@WebActivity.webView?.goBack()
                }else{
                    finishWeb()
                }
            }
            finishIv.onClick {
                finishWeb()
            }
        }

    }

    private fun pageNavigator(tag: Int) {
        if(vbIsInitiated()){
            binding?.apply {
                backIv.visibility = tag
                line.visibility = tag
            }
        }
    }

     fun initSettings() {
        webView?.apply {
            settings.javaScriptEnabled = true
            webViewClient = object : WebViewClient() {
                override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
                    view?.loadUrl(url)
                    return true
                }

                override fun onPageStarted(web: WebView?, url: String?, favicon: Bitmap?) {
                    super.onPageStarted(web, url, favicon)
                    if (vbIsInitiated()) {
                        binding.lineProgress.visibility = View.VISIBLE
                        pageNavigator(if(canGoBack()) View.VISIBLE else View.GONE)
                    }
                }

                override fun onPageFinished(web: WebView?, url: String?) {
                    super.onPageFinished(web, url)
                    if (vbIsInitiated()) {
                        binding.lineProgress.visibility = View.GONE
                    }
                }
            }

            webChromeClient = object : WebChromeClient() {
                override fun onProgressChanged(web: WebView?, progress: Int) {
                    super.onProgressChanged(web, progress)
                    if (vbIsInitiated()) {
                        binding.lineProgress.progress = progress
                    }
                }
            }
            addJavascriptInterface(this@WebActivity, "guApi")
        }
    }
    @JavascriptInterface
    fun jsCallApp(type: String?, jsonStr: String?, callback: String?): String {
        LogUtils.d("jsCallApp: type:${type}  jsonStr:${jsonStr}")
        return "{}"
    }
    override fun titleBarView(view: View?): View? {
        return binding.topToolbar
    }
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK && true == webView?.canGoBack()) {
            webView?.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event)
    }

    fun finishWeb() {
        finish()
    }
    override fun onResume() {
        super.onResume()
        webView?.onResume()
    }

    override fun onPause() {
        super.onPause()
        webView?.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        webView?.destroy()
    }


}